<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Music;
use App\Models\MusicType;
use Illuminate\Http\Request;

class MusicController extends Controller
{
    public function index()
    {

        $musics = Music::orderBy("created_at", "desc")->get();
        $musicTypes = MusicType::pluck("music_type_name", "id");
        return view("admin.music.index", [
            "musics" => $musics,
            "pageName" => "音乐列表",
            "musicTypes" => $musicTypes,
        ]);
    }

    public function create()
    {
        $musicTypes = MusicType::pluck("music_type_name", "id");
        return view("admin.music.create", [
            "pageName" => "添加音乐",
            "musicTypes" => $musicTypes,
        ]);
    }

    public function save(Request $request)
    {
        $music = new Music();
        $music->music_name = $request->post("music_name");
        $music->music_type_id = $request->post("music_type_id");
        $music->music_file = $request->post("music_file");
        $music->save();
        return redirect()->route("adminMusicIndex");

    }

    public function delete(Request $request)
    {
        $id = $request->input("id");
        Music::destroy($id);
        return redirect()->route("adminMusicIndex");

    }

    public function edit(Request $request)
    {
        $id = $request->input("id");
        $music = Music::find($id);
        $musicTypes = MusicType::pluck("music_type_name", "id");

        return view("admin.music.edit", [
            "music" => $music,
            "pageName" => "编辑音乐",
            "musicTypes" => $musicTypes,

        ]);
    }
    public function update(Request $request)
    {
        $id = $request->input("id");
        $music = Music::find($id);
        $music->music_name = $request->post("music_name");
        $music->music_type_id = $request->post("music_type_id");
        $music->music_file = $request->post("music_file");
        $music->save();
        return redirect()->route("adminMusicIndex");

    }
}
